var express = require('express')
var router = express.Router();
var pool = require('../config/mysqlconfig')
// http://localhost:3000/cate/all

router.get('/all',function(req,res,next){
    pool.getConnection(function(err,conn){
        if(err){
            console.log('数据库连接失败')
        }else{
            var  sql='select * from cate'
            conn.query(sql,function(error,results){
                if(error){
                    console.log('查询失败')
                }
                res.send(results)
                // res.send(results)
                conn.release()
            })
        }
    })
})

// 按名称查询
// http://localhost:3000/cate/select
router.post('/select',function(req,res){
    
    pool.getConnection(function(err,conn){
        if(err){
            console.log('数据库连接失败')
        }else{
            let name=req.body.c_name;
            
            let sql='select * from cate where c_name=?'
            conn.query(sql,name,function(error,results){
                if(error){
                    console.log('查询失败')
                }
                if(results){
                    // res.send({
                    //     msg:'成功',
                    //     date:results
                    // })
                    res.send(results)
                }
                conn.release()
            })
        }
    })
})
// 增加
// http://localhost:3000/cate/add
router.post('/add',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库链接失败")
        }else{
            let id = req.body.c_id;
            let name = req.body.c_name;
            let create = new Date();
            let update = new Date();

            let sql = "insert into cate set ?"
            let data = {
                c_id:id,
                c_name:name,
                create_at:create,
                updated_at:update
            }
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                    return
                }
                // if(results){
                //     // res.redirect('all')

                // }
                res.send(results)
                conn.release()
            })
        }
    })
})

// http://localhost:3000/cate/del
router.post('/del',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
            return
        }else{
            let id = req.body.c_id;
            let sql = "delete from cate where c_id = ?"
            conn.query(sql,id,function(error,results){
                if(error){
                    console.log(error)
                    conn.release()
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

// http://localhost:3000/cate/update

router.post('/update',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let id = req.body.c_id;
            let name = req.body.c_name;
            let create=new Date();
            let update=new Date();
            let data = [name,create,update,id]

            let sql = "update cate set c_name=?,create_at=?,updated_at=? where c_id=?"
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                    conn.release()
                }
                res.send(results)
            })
        }
    })
})

module.exports = router